package com.tempjob.app.controller;

import com.tempjob.app.dto.QueryUserCourseDTO;
import com.tempjob.app.dto.UserCourseWatchLogDTO;
import com.tempjob.app.security.annotation.UserLogAnnotation;
import com.tempjob.app.security.annotation.UserPassportSSO;
import com.tempjob.common.businesss.mongo.entity.UserCourse;
import com.tempjob.common.businesss.mongo.entity.UserCourseWatchLog;
import com.tempjob.common.businesss.service.MongoBeanUtil;
import com.tempjob.common.context.UserContextManage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author hui
 **/
@RequestMapping("/course")
@RestController
@Api(tags = "课程接口")
@CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})
@UserPassportSSO
public class CourseController {

    @Autowired
    private MongoBeanUtil mongoBeanUtil;

    @PostMapping("reportWatchLog")
    @ApiOperation(value = "上报观看记录")
    @UserLogAnnotation(title = "课程接口", action = "上报观看记录")
    @Transactional
    public Object industryList(@RequestBody UserCourseWatchLogDTO dto) {
        UserCourseWatchLog lastRecord = mongoBeanUtil.getUserCourseWatchLogService().lambdaQuery().eq(UserCourseWatchLog::getUserId, UserContextManage.getContext().getId()).eq(UserCourseWatchLog::getCourseId, dto.getCourseId())
                .eq(UserCourseWatchLog::getVideoId, dto.getVideoId()).orderByDesc(UserCourseWatchLog::getWatchTime).one();
        UserCourseWatchLog watchLog = new UserCourseWatchLog();
        watchLog.setCourseId(dto.getCourseId());
        watchLog.setUserId(UserContextManage.getContext().getId());
        watchLog.setVideoId(dto.getVideoId());
        watchLog.setWatchSeconds(dto.getWatchSeconds());
        watchLog.setTotalSeconds(lastRecord == null ? dto.getWatchSeconds() : lastRecord.getTotalSeconds() + dto.getWatchSeconds());
        watchLog.setWatchTime(dto.getWatchTime());
        watchLog.setUpdateTime(new Date());
        watchLog.setCreateTime(new Date());
        mongoBeanUtil.getUserCourseWatchLogService().save(watchLog);
        UserCourse userCourse = mongoBeanUtil.getUserCourseService().lambdaQuery().eq(UserCourse::getUserId, UserContextManage.getContext().getId()).eq(UserCourse::getCourseId, dto.getCourseId()).eq(UserCourse::getVideoId, dto.getVideoId()).one();
        if (userCourse == null) {
            userCourse = new UserCourse();
            userCourse.setUserId(UserContextManage.getContext().getId());
            userCourse.setCourseId(dto.getCourseId());
            userCourse.setVideoId(dto.getVideoId());
            userCourse.setCreateTime(new Date());
            userCourse.setWatchSeconds(0);
            userCourse.setTotalSeconds(0);
        }
        userCourse.setWatchSeconds(userCourse.getWatchSeconds() + dto.getWatchSeconds());
        userCourse.setTotalSeconds(userCourse.getTotalSeconds() + dto.getWatchSeconds());
        userCourse.setUpdateTime(new Date());
        mongoBeanUtil.getUserCourseService().saveOrUpdate(userCourse);
        return true;
    }

    @PostMapping("userCourseInfo")
    @ApiOperation(value = "查看总观看信息")
    @UserLogAnnotation(title = "课程接口", action = "查看总观看信息")
    @Transactional
    public Object industryList(@RequestBody QueryUserCourseDTO dto) {
        return mongoBeanUtil.getUserCourseService().lambdaQuery().eq(UserCourse::getUserId, UserContextManage.getContext().getId()).eq(UserCourse::getCourseId, dto.getCourseId()).eq(UserCourse::getVideoId, dto.getVideoId()).one();
    }

    @PostMapping("getLastWatchLog")
    @ApiOperation(value = "查看上次观看进度")
    @UserLogAnnotation(title = "课程接口", action = "查看上次观看进度")
    @Transactional
    public Object getLastWatchLog(@RequestBody QueryUserCourseDTO dto) {
        return mongoBeanUtil.getUserCourseWatchLogService().lambdaQuery().eq(UserCourseWatchLog::getUserId, UserContextManage.getContext().getId()).eq(UserCourseWatchLog::getCourseId, dto.getCourseId())
                .eq(UserCourseWatchLog::getVideoId, dto.getVideoId()).orderByDesc(UserCourseWatchLog::getWatchTime).one();
    }


}
